import tensorflow as tf
import numpy as np
row_dim = 3
col_dim = 5

# 1. 固定张量
def fixedTensor():
    # 0 张量
    zero_tsr = tf.zeros([row_dim,col_dim])
    # 1 张量
    ones_tsr = tf.ones([row_dim,col_dim])
    # 填充张量
    filled_tsr = tf.fill([row_dim,col_dim],42)
    # 固定张量
    constant_tsr = tf.constant([1,2,3])
    print(zero_tsr,'\n',ones_tsr,'\n',filled_tsr,'\n',constant_tsr)
    print('*'*30)
    # 相似形状张量
    zero_similar = tf.zeros_like(constant_tsr)
    ones_similar = tf.ones_like(filled_tsr)
    print(zero_similar,'\n',ones_similar)

# 2.序列张量
def sequenceTensor():
    # tf.linspace(start,stop,num,name)
    # linear_tsr = tf.linspace(start=0,stop=1,num=3,name='linspace')
    # print(linear_tsr)
    # like list(range(6,15,3)) but type is tensor
    integar_tsr = tf.range(6,15,3)
    print(integar_tsr)
    # random tensor
    param =  np.random.uniform(0,1,size=(row_dim,col_dim))
    # random_tsr = tf.random_uniform([row_dim,col_dim],0,1) # tensorflow2.0 lack of this function
    random_tsr = tf.constant(param)
    print(random_tsr)

# 3. tf.constant()
def tfconstant():
    '''
    tf.constant(
        value,
        dtype=None,
        shape=None,
        name='Const'
    )
    '''
    cont_tsr = tf.constant([1,2,3,4,5,6],shape=[2,3])
    print(cont_tsr)
    # it can replaced tf.fill([row_dim,col_dim],42)
    cont_broadcast_tsr = tf.constant(42,shape=[row_dim,col_dim])
    print(cont_broadcast_tsr)

'''
note:
tf.convert_to_tensor(
    value,
    dtype=None,
    dtype_hint=None,
    name=None
)
This function converts Python objects of various types to Tensor objects. 
It accepts Tensor objects, numpy arrays, Python lists, and Python scalars.
'''

# 4.运算
def tfvar():
    my_var = tf.Variable(tf.zeros([2,3]))
    # sess = tf.compat.v1.Session()
    # init_op =  tf.compat.v1.global_variables_initializer()
    # sess.run(init_op)
    m1 = tf.constant([[2,3]])
    m2 = tf.constant([[4],[5]])
    product = tf.linalg.matmul(m1,m2)
    print(product)

# 运算
def calc():
    g = tf.compat.v1.get_default_graph()
    sess = tf.compat.v1.Session(graph=g)
    identity_mat  = tf.linalg.diag([1.0,1.0,1.0])
    # print(identity_mat)
    A = tf.random.truncated_normal([2,3])
    B = tf.fill([2,3],5)
    C = tf.random.uniform([3,2])
    D = tf.convert_to_tensor(identity_mat)

    print(sess.run(tf.add(A,B)))
    sess.close()





# fixedTensor()
# sequenceTensor()
# tfconstant()
# tfvar()
calc()